<template>
  <div>
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="140px">
        <el-divider content-position="left">基础设置</el-divider>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="是否显示头像" prop="isShowImage">
              <el-radio-group v-model="form.isShowImage">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否支持头像上传" prop="sfzctx">
              <el-radio-group v-model="form.sfzctx">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示姓名" prop="isShowName">
              <el-radio-group v-model="form.isShowName">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示微信" prop="isShowWx">
              <el-radio-group v-model="form.isShowWx">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示QQ" prop="isShowQq">
              <el-radio-group v-model="form.isShowQq">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示性别" prop="isShowXb">
              <el-radio-group v-model="form.isShowXb">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示年龄" prop="isShowNl">
              <el-radio-group v-model="form.isShowNl">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示电话" prop="isShowPhone">
              <el-radio-group v-model="form.isShowPhone">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示邮箱" prop="isShowEmail">
              <el-radio-group v-model="form.isShowEmail">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示部门" prop="isShowDepart">
              <el-radio-group v-model="form.isShowDepart">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示院系" prop="isShowYx">
              <el-radio-group v-model="form.isShowYx">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示班级" prop="isShowBj">
              <el-radio-group v-model="form.isShowBj">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="8">
          <el-form-item label="是否显示图标" prop="sfxstb">
            <el-radio-group v-model="form.sfxstb">
              <el-radio
                v-for="item in this.dict.whether_type"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="使用对象" prop="yydx" :rules="[{ required: true, message: '请选择使用对象', trigger: 'change' }]">
            <el-radio-group v-model="form.yydx">
              <el-radio
                v-for="item in this.dict.service_dx"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-form>
      <div class="custom-button">
        <el-button type="primary" size="small" :loading="loading" icon="el-icon-circle-check" @click="saveEntity">保存
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { adminQueryTemplate, adminSaveOrUpdate } from '@/api/template/userCenter'
export default {
  name: 'UserCenter',
  dicts: ['whether_type', 'service_dx'],
  props: {
    template: {
      type: Number,
      require: true
    }
  },
  data() {
    return {
      loading: false,
      form: {
        templateId: this.template,
        isShowImage: 'true', // 是否显示头像
        sfzctx: 'false', // 是否支持头像上传
        isShowName: 'true', // 是否显示姓名
        isShowPhone: 'true',
        isShowEmail: 'false',
        isShowDepart: 'true',
        isShowYx: 'false',
        isShowBj: 'false',
        sfxstb: 'false',
        isShowXb: 'false',
        isShowQq: 'false',
        isShowNl: 'false',
        isShowWx: 'false',
        yydx: ''
      },
      defaultForm: {
        templateId: this.template,
        isShowImage: 'true', // 是否显示头像
        sfzctx: 'false', // 是否支持头像上传
        isShowName: 'true', // 是否显示姓名
        isShowPhone: 'true',
        isShowEmail: 'false',
        isShowDepart: 'true',
        isShowYx: 'false',
        isShowBj: 'false',
        sfxstb: 'false',
        isShowXb: 'false',
        isShowQq: 'false',
        isShowNl: 'false',
        isShowWx: 'false',
        yydx: ''
      }
    }
  },
  watch: {
    template: {
      handler(newName, oldName) {
        if (newName != null && undefined !== newName) {
          this.initEntity()
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initEntity() {
      adminQueryTemplate({ 'tid': this.template }).then(res => {
        if (res != null && res !== '') {
          this.form = res
          this.form.isShowImage = this.form.isShowImage.toString()
          this.form.sfzctx = this.form.sfzctx.toString()
          this.form.isShowName = this.form.isShowName.toString()
          this.form.isShowPhone = this.form.isShowPhone.toString()
          this.form.isShowEmail = this.form.isShowEmail.toString()
          this.form.isShowDepart = this.form.isShowDepart.toString()
          this.form.isShowYx = this.form.isShowYx.toString()
          this.form.isShowBj = this.form.isShowBj.toString()
          this.form.sfxstb = this.form.sfxstb.toString()
        } else {
          this.form = this.defaultForm
        }
      }).catch(error => {
        console.log(error)
      })
    },
    saveEntity() {
      this.loading = true
      adminSaveOrUpdate(this.form).then(res => {
        this.$message({
          message: '配置项保存成功！',
          type: 'success'
        })
        this.loading = false
      }).catch(() => {
        this.$message.error('保存失败！请联系系统管理员！')
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
.custom-button {
  width: 100%;
  height: 40px;
  text-align: center;
}
</style>
